package com.fulihui.weixinsdk.request;

import com.alibaba.fastjson.JSON;
import com.fulihui.weixinsdk.http.HttpMethodEnum;
import com.fulihui.weixinsdk.result.ApiQueryAuthResult;
import com.google.gson.Gson;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.TreeMap;

/**
 * 通过授权码和自己的接口调用凭据
 * （component_access_token），换取公众号的接口调用凭据
 * （authorizer_access_token和用于前者快过期时用来刷新它的
 * authorizer_refresh_token）和授权信息（授权了哪些权限等信息）
 *
 * Created by LeeSon on 2017/3/30 0030.
 */
@Data
public class ApiQueryAuthRequest extends WeixinRequest<ApiQueryAuthResult> {
    private String           component_appid;
    private String           authorization_code;

    private transient String component_access_token;

    @Override
    public Map<String, String> urlParam() {
        Map<String, String> param = new TreeMap<>();
        if (StringUtils.isNotBlank(component_access_token)) {
            param.put("component_access_token", component_access_token);
        }
        return param;
    }

    @Override
    protected void childParam() {

    }

    @Override
    public String service() {
        return "https://api.weixin.qq.com/cgi-bin/component/api_query_auth";
    }

    @Override
    public String urlEndStr() {
        return null;
    }

    @Override
    public ApiQueryAuthResult parseResult(String respStr) {
        return new Gson().fromJson(respStr, ApiQueryAuthResult.class);
    }

    @Override
    public HttpMethodEnum httpMethod() {
        return HttpMethodEnum.POST;
    }

    @Override
    public String requestData() {
        return JSON.toJSONString(this);
    }
}
